package com.david.func_test.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import java.lang.Math.toRadians
import kotlin.math.cos
import kotlin.math.sin

/**
 * 指南针刻度盘
 */
class CompassBgView : View {
    private val painter: Paint = Paint()

    private var radius = 0f
    private var innerR = 0f
    private var subBigInnerR = 0f
    private var bigInnerR = 0f

    private var numWidth = 0f
    private var twoNumWidth = 0f
    private var threeNumWidth = 0f

    constructor(context: Context, attr: AttributeSet) : super(context, attr) {
        painter.style = Paint.Style.STROKE
        painter.color = Color.BLACK  // 设置画笔颜色
        painter.strokeWidth = 1f     // 设置线宽
        painter.textSize = 16f

        numWidth = painter.measureText("0")
        twoNumWidth = numWidth * 2
        threeNumWidth = numWidth * 3
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        radius = measuredWidth / 2f
        innerR = radius * 0.97f
        bigInnerR = radius * 0.8f
        subBigInnerR = radius * 0.9f
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //指向底部为0度，指向右侧为90度
        for (i in 90..450) {//从90开始来让以手机底部为0/360度，左侧为90度
            //从圆中心开始画
//            canvas.drawLine(radius, radius, (radius + radius * cos(toRadians(i.toDouble()))).toFloat(),
//                (radius + radius * sin(toRadians(i.toDouble()))).toFloat(), painter)

            when {
                (i - 90) % 90 == 0 -> {//绘制长线段
                    canvas.drawLine(
                        (radius + bigInnerR * cos(toRadians(i.toDouble()))).toFloat(),
                        (radius + bigInnerR * sin(toRadians(i.toDouble()))).toFloat(),
                        (radius + radius * cos(toRadians(i.toDouble()))).toFloat(),
                        (radius + radius * sin(toRadians(i.toDouble()))).toFloat(),
                        painter
                    )

                    when (i - 90) {
                        0 -> {//在手机底部
                            canvas.drawText(
                                "0",
                                (radius + subBigInnerR * cos(toRadians(i.toDouble()))).toFloat() - numWidth / 2,
                                (radius + subBigInnerR * sin(toRadians(i.toDouble()))).toFloat() - 70,//减80防止覆盖到线上
                                painter
                            )
                        }

                        90 -> {//在手机左侧
                            canvas.drawText(
                                "90",
                                (radius + subBigInnerR * cos(toRadians(i.toDouble()))).toFloat() + 100,//加100防止覆盖到线上
                                (radius + subBigInnerR * sin(toRadians(i.toDouble()))).toFloat() + numWidth / 2,
                                painter
                            )
                        }

                        180 -> {//在手机上部
                            canvas.drawText(
                                "180",
                                (radius + subBigInnerR * cos(toRadians(i.toDouble()))).toFloat() - threeNumWidth / 2,
                                (radius + subBigInnerR * sin(toRadians(i.toDouble()))).toFloat() + 100,//加100 防止覆盖到线上
                                painter
                            )
                        }

                        270 -> {//在手机右侧
                            canvas.drawText(
                                "270",
                                (radius + subBigInnerR * cos(toRadians(i.toDouble()))).toFloat() - 100,//减100防止覆盖到线上
                                (radius + subBigInnerR * sin(toRadians(i.toDouble()))).toFloat() + numWidth / 2,
                                painter
                            )
                        }

                        360 -> {//跟0度一样在手机底部
                            canvas.drawText(
                                "360",
                                (radius + subBigInnerR * cos(toRadians(i.toDouble()))).toFloat() - threeNumWidth / 2,
                                (radius + subBigInnerR * sin(toRadians(i.toDouble()))).toFloat() - 100,
                                painter
                            )
                        }
                    }
                }

                i % 30 == 0 -> {//绘制中等长度线段
                    canvas.drawLine(
                        (radius + subBigInnerR * cos(toRadians(i.toDouble()))).toFloat(),
                        (radius + subBigInnerR * sin(toRadians(i.toDouble()))).toFloat(),
                        (radius + radius * cos(toRadians(i.toDouble()))).toFloat(),
                        (radius + radius * sin(toRadians(i.toDouble()))).toFloat(),
                        painter
                    )
                }

                else -> {//绘制短线段
                    canvas.drawLine(
                        (radius + innerR * cos(toRadians(i.toDouble()))).toFloat(),
                        (radius + innerR * sin(toRadians(i.toDouble()))).toFloat(),
                        (radius + radius * cos(toRadians(i.toDouble()))).toFloat(),
                        (radius + radius * sin(toRadians(i.toDouble()))).toFloat(),
                        painter
                    )
                }
            }
        }
    }
}